<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

$race_team_id = -1;
$gold = -1;
if( isset( $_POST[ 'gold' ] ) ) {
	$gold = formatInput( $_POST[ 'gold' ] );
	if( !is_numeric( $gold ) ) {
		$gold = -1;
	}
}
if( isset( $_POST[ 'team_id' ] ) ) {
	$race_team_id = formatInput( $_POST[ 'team_id' ] );
}
if( is_numeric( $race_team_id ) ) {
	$query = "SELECT count(*) FROM race_teams WHERE race_team_id = $race_team_id";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	if( $row[ 'count(*)' ] > 0 ) {
		$current_turn = getCurrentTurn();
		$query = "SELECT race_id FROM races WHERE turn > $current_turn LIMIT 1";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array( $result, MYSQL_ASSOC );
		if( isset( $row[ 'race_id' ] ) ) {
			$race_id = $row[ 'race_id' ];
			
			$query = "SELECT SUM(gold) " .
					"FROM race_bets " .
					"WHERE race_id = $race_id AND race_team_id = $race_team_id AND ruler_id = " . $_SESSION[ 'ruler_id' ];
			$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$row = mysql_fetch_array( $result, MYSQL_ASSOC );
			if( $row[ 'SUM(gold)' ] + $gold > 100000 ) {
				$status = "You can't bet more than <strong class=\"gold\">100,000</strong> gold per race per team.";
				$rest = 100000 - $row[ 'SUM(gold)' ];
				if( $rest > 0 ) {
					$status .= "<br />\n" .
							"You can only bet <strong class=\"gold\">" . number_format( $rest ) . "</strong> gold more on this team for the coming race.";
				}
			}
			else {
				$query = "SELECT gold FROM ruler WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
				$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
				$row = mysql_fetch_array( $result, MYSQL_ASSOC );
				$ruler_gold = $row[ 'gold' ];
				
				if( $gold < 10 ) {
					$status = "You must at least bet <strong class=\"gold\">10</strong> gold.";
				}
				else if( $ruler_gold >= $gold ) {
					$query = "UPDATE ruler SET gold = " . ( $ruler_gold - $gold ) . " WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
					mysql_query( $query ) or die( "Query failed : " . mysql_error() );
					
					$race_gold = round( 0.02 * $gold );
					
					$query = "INSERT INTO race_bets ( ruler_id, race_id, gold, race_team_id )" .
							"VALUES ( " . $_SESSION[ 'ruler_id' ] . ", $race_id, $gold, $race_team_id )";
					mysql_query( $query ) or die( "Query failed : " . mysql_error() );
					
					$query = "SELECT prizes FROM races WHERE race_id = " . $race_id;
					$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
					$row = mysql_fetch_array( $result, MYSQL_ASSOC );
					
					$query = "UPDATE races SET prizes = " . ( $row[ 'prizes' ] + $race_gold ) . " WHERE race_id = $race_id";
					mysql_query( $query ) or die( "Query failed : " . mysql_error() );
					
					$status = "Bet placed!";
				}
				else {
					$status = "You don't have <strong class=\"gold\">" . number_format( $gold ) . "</strong> gold. (Only <strong class=\"gold\">" . number_format( $ruler_gold ) . "</strong> gold)";
				} 
			}
		}
		else {
			$status = "You can't place a bet at the moment, because there are no races scheduled.";
		}
	}
	else {
		$status = "Unknown team id supplied! $race_team_id";
	}
}
else {
	$status = "Wrong team id supplied.";
}

if( isset( $status ) ) {
	$_SESSION[ 'place_bet_status' ] = $status;
}
header( "Location: horseraces.php" );
?>